﻿using System.Data.SQLite;

namespace RobosimAPI.Class
{
	public class Map
	{
        /*
		 CREATE TABLE themeRuleDetails (
        id         INTEGER,
        title      TEXT,
        difficulty INTEGER,
        routine    INTEGER,
        robot      TEXT,
        program    TEXT,
        sort       INTEGER,
        [limit]    TEXT,
        md5        TEXT,
        map_pwd    TEXT,
        map_url    TEXT,
        theme_rule INTEGER
        );
		*/
        public int id;
        public string? title;
        public int difficulty;
        public int routine;
        public string? robot;
        public string? program;
        public int sort;
        public string? limit;
        public string? md5;
        public string? map_pwd;
        public string? map_url;
        public int theme_rule;
        public static Map[] GetMapsWhereRuleId(int rule_id)
        {
            var maps = new List<Map>();
            using (var conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
            {
                conn.Open();
                var cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM themeRuleDetails WHERE theme_rule = @rule_id";
                cmd.Parameters.AddWithValue("@rule_id", rule_id);
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var map = new Map
                        {
                            id = reader.GetInt32(0),
                            title = reader.IsDBNull(1)? null : reader.GetString(1),
                            difficulty = reader.GetInt32(2),
                            routine = reader.GetInt32(3),
                            robot = reader.IsDBNull(4)? null : reader.GetString(4),
                            program = reader.IsDBNull(5)? null : reader.GetString(5),
                            sort = reader.GetInt32(6),
                            limit = reader.GetString(7),
                            md5 = reader.GetString(8),
                            map_pwd = reader.GetString(9),
                            map_url = reader.GetString(10),
                            theme_rule = reader.GetInt32(11)
                        };
                        maps.Add(map);
                    }
                }
				conn.Close();
                conn.Dispose();
			}
            return maps.ToArray();
        }
	}
}
